<?php
/**
 * Class Infrastructure_Mapper_AbstractBase
 */
class Infrastructure_Mapper_AbstractBase
{
    /**
     * @param       $query
     * @param array $params
     * @return PDOStatement
     * @throws Infrastructure_Exception_Database
     */
    public function query($query, array $params = array())
    {
        $pdoStatement = $this->pdo->prepare($query);
        if ($pdoStatement === false) {
            $error = $this->pdo->errorInfo();
            throw new Infrastructure_Exception_Database("DATABASE ERROR - " . print_r($error, true));
        }
        $res = $pdoStatement->execute($params);
        if ($res === false) {
            $error = $pdoStatement->errorInfo();
            throw new Infrastructure_Exception_Database("DATABASE ERROR - " . print_r($error, true));
        }
        return $pdoStatement;
    }

    /**
     * @param string $field
     * @param string $table
     * @return int
     */
    protected function getNewId($field, $table)
    {
        $res = $this->query("SELECT MAX(`" . $field . "`) + 1 AS newId FROM " . $table);
        $obj = $res->fetchObject();
        return $obj->newId;
    }

    /**
     * @param PDO $pdo
     */
    public function __construct(PDO $pdo)
    {
        $this->pdo = $pdo;
    }
}